<?php

  include "dbfunc.php";
  require_once 'functions.php';
  $dbconn=init_db();
  session_start();
  
  if(!empty($_POST["reqType"]) && $_POST["reqType"]=="logon"){
      $result=user_validate($_POST["email"],$_POST["password"]);
	  if(!$result){
	   die("failed");
	  }
	  else{
	   $row=mysql_fetch_array($result);
	   if($row['locked']==1){
	   	die('locked');
	   }
	   $_SESSION["user"]=$row;
	   exit("sucess");
	  }
  }
  
  if(!empty($_POST["reqType"]) && $_POST["reqType"]=="checkmail"){
     if(mail_is_used($_POST["email"])){
       die("failed");
      }
      else{
       exit("sucess");
      }
  }
  
  
  if(!empty($_POST["reqType"]) && $_POST["reqType"]=="register"){
  	  if($_POST["invitecode"]!="金灯台"){
  	  	die("failed");
  	  }
      $result=mysql_query("insert into users (name,passwd,email,sex,portrait) values"."('"
            .$_POST["username"]."','".$_POST["password"]."','".$_POST["email"]."','".$_POST["sex"]."','".$_POST["portraitid"]."')");
      if(!$result){
       die("failed");
      }
      else{
       $result=user_validate($_POST["email"],$_POST["password"]);
       $row=mysql_fetch_array($result);
       $_SESSION["user"]=$row;
       $post_content='<span style="margin-left:10%;float:left"><img src="'.get_portrait_url($_POST["portraitid"],"thumb").'"></span><span style="margin-left:5%;float:left">'.$_POST["username"]."加入这个大家庭。大家欢迎！</span>";
       create_post(array("type"=>9,"userid"=>get_system_user_id(),"content"=>$post_content));
       $insert_id=mysql_insert_id();
       set_post_addi($insert_id,"flower","");
       exit("success");
      }
  }
?>